{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**依赖的库安装**\n",
    "\n",
    "```python\n",
    "- transformers==4.42.4\n",
    "\n",
    "- peft==0.11.1\n",
    "\n",
    "- datasets==2.20.0\n",
    "\n",
    "- accelerate==0.32.1\n",
    "\n",
    "- bitsandbytes==0.43.1\n",
    "\n",
    "- faiss-cpu==1.7.4\n",
    "\n",
    "- tensorboard==2.14.0\n",
    "\n",
    "-pytorch：根据自己电脑\n",
    "```\n",
    "\n",
    "**修改hosts**\n",
    "\n",
    "路径：C:\\Windows\\System32\\drivers\\etc\n",
    "\n",
    "防止从github下载模型遇到阻碍\n",
    "\n",
    "```tex\n",
    "185.199.108.133 raw.githubusercontent.com\n",
    "185.199.109.133 raw.githubusercontent.com185.199.110.133 raw.githubusercontent.com\n",
    "185.199.111.133 raw.githubusercontent.com2606:50c0:8000::154 raw.githubusercontent.com\n",
    "2606:50c0:8001::154 raw.githubusercontent.com\n",
    "2606:50c0:8002::154 raw.githubusercontent.com\n",
    "2606:50c0:8003::154 raw.githubusercontent.com\n",
    "\n",
    "```\n",
    "\n",
    "#### "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 样例1: 情感分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "# 导入transformers相关包\n",
    "from transformers import pipeline\n",
    "# 通过Interface加载pipline并启动文本分类任务\n",
    "\"\"\"下载与加载\"\"\"\n",
    "model_load=pipeline(\n",
    "    \"text-classification\",#任务\n",
    "    model=\"uer/roberta-base-finetuned-dianping-chinese\",#加载的与训练模型名称\n",
    "    model_kwargs={\"cache_dir\":\"models_saved\",#自定义缓存目录\n",
    "                  \"local_files_only\": True #下载完后，二次加载使用本地下载后的模型\n",
    "                  },\n",
    "    device=0\n",
    "                  \n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**创建GRadio界面**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7861\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.Interface.from_pipeline(\n",
    "    model_load\n",
    ").launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 样例2 阅读理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3775ffe58e148728946eeecfbdc3613",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/452 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "z:\\ANACONDA\\envs\\transformers\\lib\\site-packages\\huggingface_hub\\file_download.py:142: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in Z:\\PYwork\\HUgging_face_study\\models_saved\\models--uer--roberta-base-chinese-extractive-qa. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.\n",
      "To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development\n",
      "  warnings.warn(message)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e1981979e40e48618b93a086946145ec",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "pytorch_model.bin:   0%|          | 0.00/407M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "acf3ca0195e446108cceac9c6c44ec29",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/216 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0b1668ecf98f4c719ef6698b45de986d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.txt:   0%|          | 0.00/110k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5c50033c9b304b0384727b6e518f5d20",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "special_tokens_map.json:   0%|          | 0.00/112 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Hardware accelerator e.g. GPU is available in the environment, but no `device` argument is passed to the `Pipeline` object. Model will be on CPU.\n"
     ]
    }
   ],
   "source": [
    "model_qa=pipeline(\"question-answering\",\n",
    "                  model=\"uer/roberta-base-chinese-extractive-qa\",\n",
    "                  model_kwargs={\"cache_dir\":\"models_saved\",         \n",
    "                                }\n",
    "                  \n",
    "                  )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7862\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.Interface.from_pipeline(model_qa).launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "transformers",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
